﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.RAL.Access;
using Empires.RAL.ORM;
using Empires.CRL.DataContract;

namespace Empires.RAL.Pump
{
    public class AppBlogDataPump : IAppBlogAccess
    {
        public DefaultDataPack GetArticle(int id)
        {
            using (mysql_empiresEntities ctx = new mysql_empiresEntities())
            {
                var article = (from c in ctx.tb_blog
                               where c.id == id
                               select new
                               {
                                   c.id,
                                   c.c_dt_date,
                                   c.c_i_scan,
                                   c.tb_user.c_nvc_nickname,
                                   c.c_nvc_content,
                                   c.c_nvc_title,
                                   c.tb_blogreply.Count,
                                   c.c_nvc_mainphoto
                               }).FirstOrDefault();
                if (article != null)
                {
                    return new DefaultDataPack()
                    {
                        ID = article.id,
                        DATE = article.c_dt_date.ToLongDateString(),
                        AUTHOR = article.c_nvc_nickname,
                        HIT = article.c_i_scan,
                        CONTENT = article.c_nvc_content,
                        TITLE = article.c_nvc_title,
                        REPLYTIME = article.Count,
                        URL = article.c_nvc_mainphoto
                    };
                }
                return null;
            }
        }
        public List<DefaultDataPack> GetArticleReply(int articleId, int page, int takeitem)
        {
            using (mysql_empiresEntities ctx = new mysql_empiresEntities())
            { 
                var itemCount = ctx.tb_blogreply.Where(c=>c.c_i_blogid == articleId).Count();
                var list = (from c in ctx.tb_blogreply
                            where c.c_i_blogid == articleId
                            orderby c.id descending
                            select new
                            {
                                c.c_nvc_author,
                                c.c_dt_replayDate,
                                c.c_nvc_content
                            })
                           .Skip(page * takeitem)
                           .Take(takeitem)
                           .AsEnumerable()
                           .Select(c =>
                               {
                                   return new DefaultDataPack()
                                   {
                                       AUTHOR = c.c_nvc_author,
                                       DATE = c.c_dt_replayDate.ToLongDateString(),
                                       CONTENT = c.c_nvc_content,
                                       ITEMCOUNT = itemCount
                                   };
                               }).ToList();
                return list;
            }
        }

        public int PostArticle(DateTime pbdate, string author, string title, string mainimg,string shortimg, string content)
        {
            Random ran = new Random();
            using (mysql_empiresEntities ctx = new mysql_empiresEntities())
            {
                return ctx.PostBlogArticle(pbdate, author, title, content, ran.Next(10, 100),
                         mainimg, shortimg).FirstOrDefault();
                //return ctx.ExecuteStoreCommand("call proc_PostBlogArticle(@pbdate,@author,@title,@content,@scan,@isLock,@photo)",
                //    pbdate, author, title, ran.Next(10, 100), 0, mainimg);
                //ctx.ExecuteFunction("proc_PostBlogArticle",
                //    new System.Data.Objects.ObjectParameter("pbdate", pbdate),
                //    new System.Data.Objects.ObjectParameter("author", author),
                //    new System.Data.Objects.ObjectParameter("title", title),
                //    new System.Data.Objects.ObjectParameter("scan", ran.Next(10, 100)),
                //    new System.Data.Objects.ObjectParameter("isLock", 0),
                //    new System.Data.Objects.ObjectParameter("photo", mainimg));
                //return ctx.CreateQuery<int>("call proc_PostBlogArticle(@pbdate,@author,@title,@content,@scan,@isLock,@photo)",
                //    new System.Data.Objects.ObjectParameter[]{
                //        ,
                //    }).FirstOrDefault();
            }
        }
        public int PostArticleReply(int articleId, string author, string content)
        {
            using (mysql_empiresEntities ctx = new mysql_empiresEntities())
            {
                ctx.tb_blogreply.AddObject(
                    new tb_blogreply()
                    {
                        c_dt_replayDate = DateTime.Now,
                        c_i_blogid = articleId,
                        c_nvc_author = author,
                        c_nvc_content = content
                    });
                return ctx.SaveChanges();
            }
        }
        public int EditArticle(int id, string author, string title, string content)
        {
            using (mysql_empiresEntities ctx = new mysql_empiresEntities())
            {
                var article = ctx.tb_blog.Where(c => c.id == id).FirstOrDefault();
                article.c_nvc_author = author;
                article.c_nvc_title = title;
                article.c_nvc_content = content;
                return ctx.SaveChanges();
            }
        }

        public int DeleteArticle(int id)
        {
            using (mysql_empiresEntities ctx = new mysql_empiresEntities())
            {
                var article = ctx.tb_blog.Where(c => c.id == id).FirstOrDefault();
                if (article != null)
                {
                    article.c_b_isLock = true;
                    return ctx.SaveChanges();
                }
            }
            return 0; 
        }

        /// <summary>
        /// ID = item.id,
        /// DATE = item.c_dt_date.ToLongDateString(),
        /// AUTHOR = item.c_nvc_author,
        /// HIT = item.c_i_scan,
        /// ABSTRACT = content, 截取前200个字符
        /// ItemCount = itemcount
        /// </summary>
        /// <param name="page"></param>
        /// <param name="takeitem"></param>
        /// <returns></returns>
        public IEnumerable<DefaultDataPack> GetBlogs(int page, int takeitem)
        {
            using (mysql_empiresEntities ctx = new mysql_empiresEntities())
            {
                var itemcount = ctx.tb_blog.Count();
                var articles = (from c in ctx.tb_blog
                                orderby c.id descending
                                select
                                new
                                {
                                    c.id,
                                    c.c_dt_date,
                                    c.c_i_scan,
                                    c.c_nvc_content,
                                    c.tb_user.c_nvc_nickname,
                                    replytime = c.tb_blogreply.Count,
                                    c.c_nvc_mainphoto,
                                    c.c_nvc_title
                                })
                                .Skip(page * takeitem)
                                .Take(takeitem)
                                .AsEnumerable();
                foreach (var item in articles)
                { 
                    string content = Empires.CRL.Tools.StringPlus.GetStringWithoutHTMLLabel(item.c_nvc_content.Replace("&lt;","<").Replace("&gt;",">"));
                    if(!String.IsNullOrEmpty(content))
                    {
                        if(content.Length > 200)
                        {
                            content = content.Substring(0,200) + "．．．．．．";
                        }                       
                    }
                    else
                    {
                        content = "";
                    }
                    yield return new DefaultDataPack()
                    {
                        ID = item.id,
                        DATE = item.c_dt_date.ToLongDateString(),
                        AUTHOR = item.c_nvc_nickname,
                        HIT = item.c_i_scan,
                        ABSTRACT = content,
                        ITEMCOUNT = itemcount,
                        REPLYTIME = item.replytime,
                        URL=item.c_nvc_mainphoto,
                        TITLE = item.c_nvc_title
                    };
                }
            }
        }

        /// <summary>
        /// ID = item.id,
        /// DATE = item.c_dt_date.ToLongDateString(),
        /// ABSTRACT = content 截取前200个字符
        /// </summary>
        /// <param name="takeitem"></param>
        /// <returns></returns>
        public IEnumerable<DefaultDataPack> GetPopularBlogs(int takeitem)
        {
            using (mysql_empiresEntities ctx = new mysql_empiresEntities())
            {
                var articles = (from c in ctx.tb_blog
                                orderby c.c_i_scan descending
                                select
                                new
                                {
                                    c.id,
                                    c.c_dt_date,
                                    c.c_nvc_content,
                                    c.c_nvc_stphoto
                                })
                                .Take(takeitem)
                                .AsEnumerable();
                string content = string.Empty;
                foreach (var item in articles)
                {
                    content = Empires.CRL.Tools.StringPlus.GetStringWithoutHTMLLabel(item.c_nvc_content.Replace("&lt;", "<").Replace("&gt;", ">"));
                    if (!String.IsNullOrEmpty(content))
                    {
                        if (content.Length > 30)
                        {
                            content = content.Substring(0, 30) + "．．．．．．";
                        }
                    }
                    else
                    {
                        content = "";
                    }
                    yield return new DefaultDataPack()
                    {
                        ID = item.id,
                        DATE = item.c_dt_date.ToLongDateString(),
                        TITLE = content,
                        URL=item.c_nvc_stphoto
                    };
                }
            }
        }


        
    }
}
